package org.hbhk.hxl.gateway.services;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

public class IpMonitor implements InitializingBean {

	private ScheduledExecutorService service = Executors.newScheduledThreadPool(10);

	public static boolean isHostConnectable(String host, int port) {
		Socket socket = new Socket();
		try {
			socket.connect(new InetSocketAddress(host, port), 3000);
			boolean res = socket.isConnected();
			return res;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static boolean isHostReachable(String host, Integer timeOut) {
		try {
			return InetAddress.getByName(host).isReachable(timeOut);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	private Logger log = LoggerFactory.getLogger(getClass());

	@Override
	public void afterPropertiesSet() throws Exception {
		service.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				try {

					Map<String, List<String>> nodeCache = HmsDefaultService.nodeCache;
					if (nodeCache.size() > 0) {
						
						
					}

					Map<String, List<String>> grayNodeCache = HmsDefaultService.grayNodeCache;

					if (grayNodeCache.size() > 0) {

					}

				} catch (Exception e) {
					log.error("节点监控信息错误", e);
				}
			}

		}, 60, 60, TimeUnit.SECONDS);
	}

}
